package com.example.filetest;

//使用hashmap或者treemap进行遍历统计

import java.util.Map;
import java.util.StringJoiner;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class Test03 {
    public static void main(String[] args) {

        String str = "abcdeabcdeabcde";

        TreeMap<Character, Integer> treeMap = new TreeMap<>();

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (treeMap.containsKey(c)) {
                Integer count = treeMap.get(c);
                count++;
                treeMap.put(c, count);
            } else {
                treeMap.put(str.charAt(i), 1);
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        treeMap.forEach(new BiConsumer<Character, Integer>() {
            @Override
            public void accept(Character key, Integer value) {
                stringBuilder.append(key).append('(').append(value).append(')');
            }
        });

        StringJoiner stringJoiner = new StringJoiner("");

        for (Map.Entry<Character, Integer> entry : treeMap.entrySet()) {
            stringJoiner.add(entry.getKey().toString()).add("(").add(entry.getValue().toString()).add(")");
        }
        System.out.println(stringJoiner);
        System.out.println(stringBuilder);

    }
}
